﻿using System.Collections.Generic;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using testWX.Data;
namespace testWX.Service.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("any")] //设置跨域处理的 代理
    public class EmployeeController : ControllerBase
    {
        private readonly IEmployeeProvider _employeeProvider;
        private readonly IEmployeeProcessor _employeeProcessor;
        /// <summary>
        /// 测试employee
        /// </summary>
        /// <param name="employeeProvider"></param>
        /// <param name="employeeProcessor"></param>
        public EmployeeController(IEmployeeProvider employeeProvider, IEmployeeProcessor employeeProcessor)
        {
            _employeeProvider = employeeProvider;
            _employeeProcessor = employeeProcessor;
        }
        /// <summary>
        /// 获取全部employee
        /// </summary>
        /// <returns></returns>
        // GET: api/Employee
        [HttpGet]
        public IEnumerable<Employee> Get()
        {
            return _employeeProvider.Get();
        }
        /// <summary>
        /// 通过给出id读取指定的employee
        /// </summary>
        /// <param name="id">employee的id</param>
        /// <returns></returns>
        // GET: api/Employee/5
        [HttpGet("{id}", Name = "Get")]
        public Employee Get(int id)
        {
            return _employeeProvider.Single(id);
        }
        /// <summary>
        /// 新建employee
        /// </summary>
        /// <param name="employee"></param>
        // POST: api/Employee
        [HttpPost]
        public void Post([FromBody] Employee employee)
        {
            _employeeProcessor.Create(employee);
        }

        /// <summary>
        /// 通过给出id更新已存在的指定employee
        /// </summary>
        /// <param name="id">employee的id</param>
        /// <param name="employee"></param>
        // PUT: api/Employee/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] Employee employee)
        {
            employee.Id = id;
            _employeeProcessor.Update(employee);
        }
        /// <summary>
        /// 通过给出id删除已存在的指定employee
        /// </summary>
        /// <param name="id">employee的id</param>
        // DELETE: api/ApiWithActions/5
        //[Route("~/api/Delete/{id}")]
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            _employeeProcessor.Delete(id);
        }
    }
}
